package com.example.im.monitor

import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.listener.LKIMMessageListener
import com.example.im.IMReceiveService
import com.example.im.distribute.CMDMessageDistribute
import com.example.im.distribute.DefMessageDistribute

/**
 * 消息监听
 * @author  chaohaipeng  on 2018/7/23.
 */
class MessageMonitor: LKIMMessageListener {
    private object mHolder {
        val instance = MessageMonitor()
    }
    companion object {
        fun getInstance(): MessageMonitor {
            return mHolder.instance
        }
    }
    /**
     * 消息类撤回通知(没有使用)
     *
     * @param lkimMessages 消息体集合
     */
    override fun onMessageRecalled(p0: MutableList<LKIMMessage>?) {
    }
    /**
     * 消息状态变动通知(没有使用)
     *
     * @param lkimMessage 消息体
     * @param change
     */
    override fun onMessageChanged(p0: LKIMMessage?, p1: Any?) {
    }
    /**
     * CMD消息接收通知
     *
     * @param lkimMessages 消息体集合
     */
    override fun onCmdMessageReceived(lkimMessages: List<LKIMMessage>?) {
        if (lkimMessages!=null){
            CMDMessageDistribute.getInstance().distribute(lkimMessages)
        }
    }
    /**
     * 消息接收通知
     *
     * @param lkimMessages 消息体集合
     */
    override fun onMessageReceived(p0: List<LKIMMessage>?) {
        DefMessageDistribute.getInstance().distribute(p0)
    }

    /**
     * 消息已送达回执(没有使用)
     *
     * @param lkimMessages 消息体集合
     */
    override fun onMessageDelivered(p0: MutableList<LKIMMessage>?) {
    }
    /**
     * 消息已读接收通知(单聊)
     *
     * @param lkimMessages 消息体集合
     */
    override fun onMessageRead(lkimMessages: List<LKIMMessage>?) {
        if (lkimMessages!=null){
            for (lkimMessage in lkimMessages) {
                IMReceiveService.getInstance().sendRefreshMessage(lkimMessage.getToAccount(), lkimMessage.getMsgId(), lkimMessage)
            }
        }

    }
}